package com.antexpress.service;

import com.antexpress.bean.Page;
import com.antexpress.common.Response;
import com.antexpress.dao.DepartmentDAO;
import com.antexpress.dao.EmployeeDAO;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.Title;
import com.antexpress.util.DataUtil;
import com.antexpress.util.MD5Util;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Andy
 */
@Service
public class EmployeeServiceImpl implements EmployeeService{
    @Autowired
    private EmployeeDAO empDAO;
    @Autowired
    private DepartmentDAO deptDAO;
    
    @Override
    public Response login(String username, String password) {
        Response resp = null;
        Employee emp = empDAO.getEmployeeByUsername(username);
        if(emp!=null && emp.getPassword().equals(MD5Util.MD5EncodeUtf8(password))&&emp.getStatus()>=0){
            resp = Response.createBySuccess(emp);
        }else{
            resp = Response.createByErrorMessage("Incorrect username or password!");
        }
        return resp;
    }

    @Override
    public Response updateProfile(Employee emp) {
        Response resp = null;
        emp.setUpdatetime(DataUtil.getTimestamp());
        if(empDAO.updateEmployee(emp)){
            resp = Response.createBySuccessMessage("Save success!");
        }else {
            resp = Response.createByErrorMessage("Save failed!");
        }
        return resp;
    }

    @Override
    public Response empForPage(String name, String mobile, String dept, String title,int currentPage, int pageSize) {
        Response resp = null;
        Page page = new Page();
        //总记录数
        int allRow = 0;
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Employee> list = null;
        if(title!=null&&!title.equals("")){
            Title t = deptDAO.getTitleById(title);
            list = empDAO.listEmpForPage(name, mobile, t, offset, pageSize); 
            allRow = empDAO.getRowCountByTitle(name, mobile, t);
        }else if(dept!=null&&!dept.equals("")){
            list = new ArrayList<Employee>();
            List<Title> titles = deptDAO.getTitleListByDept(dept);
            if(!titles.isEmpty()){
                list = empDAO.listEmpForPageByTitles(name, mobile, titles, offset, pageSize);
                allRow = empDAO.getRowCountByTitles(name, mobile, titles);
            }
        }else{
            list = empDAO.listEmpForPage(name, mobile, null, offset, pageSize); 
            allRow = empDAO.getRowCountByTitle(name, mobile, null);
        }
            
        for(Employee emp : list){
            emp.setPassword(null);
        }
        
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        resp = Response.createBySuccess(page);
        return resp;
    }

    @Override
    public Response addEmp(Employee emp) {
        Response resp = null;
        boolean isSuccess = empDAO.addEmployee(emp);
        if(isSuccess)
            resp = Response.createBySuccess();
        else
            resp = Response.createByErrorMessage("Added failed!");
        return resp;
    }

    @Override
    public boolean isEmailExisted(String email) {
        boolean isExisted = empDAO.isEmailExisted(email);
        return isExisted;
    }

    @Override
    public boolean isUsernameExisted(String username) {
        boolean isExisted = empDAO.isUsernameExisted(username);
        return isExisted;
    }

    @Override
    public Employee getEmpById(String id) {
        return empDAO.getEmployeeById(id);
    }

    @Override
    public Response batchDeleteEmp(String[] ids) {
        Response resp = null;
        List<Employee> delEmp = new ArrayList<Employee>();
        for(String id:ids){
            Employee emp = empDAO.getEmployeeById(id);
            if(emp!=null){
                emp.setStatus(-10);
                emp.setUpdatetime(new Date());
                delEmp.add(emp);
            }
        }
        boolean isSuccess = empDAO.batchUpdateEmp(delEmp);
        if(isSuccess)
            resp =  Response.createBySuccessMessage("Delete success!");
        else
            resp =  Response.createByErrorMessage("Opps, something goes error!");
        return resp;
    }
    
    @Override
    public int getInServiceEmpNum() {
        if(empDAO==null)
            return 1;
        return empDAO.getInServiceEmpNum();
    }

    @Override
    public Response getHomeEmpMsg() {
        int inService = empDAO.getInServiceEmpNum();
        int leaving = empDAO.getLeavingEmpNum();
        Map data = new HashMap();
        data.put("service", inService);
        data.put("leaving", leaving);
        return Response.createBySuccess(data);
    }
}
